<template>
  <div class="list-table">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:income:edit']"
          type="warning"
          icon="el-icon-edit"
          size="mini"
          @click="batchEditHandle"
        >{{ $t("编辑") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="batchEdit"
          size="mini"
          @click="cancel"
        >{{ $t("取消") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="batchEdit"
          v-hasPermi="['fund:income:edit']"
          type="primary"
          size="mini"
          @click="batchSaveHandle"
        >{{ $t("保存") }}</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="fundIncomeDetailList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('出资人名称')" align="center" prop="userName" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('出资人类型')" align="center" prop="investorsType" :formatter="investorsTypeOptionsFormat" min-width="140" show-overflow-tooltip />
      <el-table-column :label="$t('计算基数') + $t('(元/%)')" align="center" prop="calculationBase" width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.calculationBase, 'rate') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="amountPrincipal" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="!batchEdit">{{ moneyFormat(scope.row.amountPrincipal, 'w') }}</span>
          <Money v-else v-model="scope.row.amountPrincipal" :placeholder="$t('本金')" unit="w" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('收益') + $t('金额单位')" align="center" prop="amountIncome" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="!batchEdit">{{ moneyFormat(scope.row.amountIncome, 'w') }}</span>
          <Money v-else v-model="scope.row.amountIncome" :placeholder="$t('收益')" unit="w" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="amountBonus" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="!batchEdit">{{ moneyFormat(scope.row.amountBonus, 'w') }}</span>
          <Money v-else v-model="scope.row.amountBonus" :placeholder="$t('分红')" unit="w" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('其他') + $t('金额单位')" align="center" prop="amountOthers" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="!batchEdit">{{ moneyFormat(scope.row.amountOthers, 'w') }}</span>
          <Money v-else v-model="scope.row.amountOthers" :placeholder="$t('分红')" unit="w" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('总额') + $t('金额单位')" align="center" prop="amountDistribution" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.amountDistribution, 'w') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listFundIncomeDetail, batchSaveIncomeDetail } from '@/api/fund/incomeDetail/fundIncomeDetail'
export default {
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 基金收益或项目回款明细表格数据
          fundIncomeDetailList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
          // 批量编辑
          batchEdit: false,
          investorsTypeOptions: [],
          // 查询参数
          queryParams: {
              incomeId: undefined,
              dealDistributionDetailsId: undefined,
              investorId: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      // 出资人类型
      this.getDicts('fund_investor_type').then(response => {
        response.data.forEach(item => {
          this.investorsTypeOptions.push(item)
        })
      })
  },
  methods: {
    /** 查询基金收益或项目回款明细列表 */
    getList() {
        this.loading = true
        listFundIncomeDetail(this.queryParams).then(response => {
            this.fundIncomeDetailList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 出资人类型
    investorsTypeOptionsFormat(row) {
      return this.selectDictLabel(this.investorsTypeOptions, row.investorsType)
    },
    getfundIncomeDetail(row) {
      this.loading = true
      this.queryParams.incomeId = row.id
      listFundIncomeDetail(this.queryParams).then(response => {
        this.fundIncomeDetailList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            incomeId: undefined,
            fundId: undefined,
            dealDistributionDetailsId: undefined,
            investorId: undefined,
            calculationBase: undefined,
            amountPrincipal: undefined,
            amountIncome: undefined,
            amountBonus: undefined,
            amountOthers: undefined,
            amountDistribution: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
        this.resetForm('form')
    },
    /**
     * 批量编辑
     */
    batchEditHandle() {
      this.loading = true
      this.batchEdit = true
      this.loading = false
    },
    /**
     * 取消
     */
    cancel() {
      this.loading = true
      this.getList()
      this.batchEdit = false
      this.loading = false
    },
    /**
     * 批量保存
     */
    batchSaveHandle() {
      this.loading = true
      batchSaveIncomeDetail(this.fundIncomeDetailList).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
        } else {
          this.msgError(response.msg)
        }
        this.getList()
        this.loading = false
        this.batchEdit = false
      }).catch(() => {
        this.loading = false
        this.batchEdit = false
      })
    }
  }
}
</script>
